import npmLogin from "npm-cli-login"
import {exce} from "./cmd"

/**
 * npm login
 * @param {{ registry:string, scope: string, username: string, password: string, userconfig:string}} args 
 */
const login = (args) => {
    const {username, password, registry, scope, userconfig} = args;
    const email = "packages-truck@example.com"
    try{
        npmLogin(username, password, email, registry, scope, false, userconfig)
        return {
            isSuccess: true
        }
    }catch(err){
        return {
            isSuccess: false,
            msg: err
        }
    }
}

/**
 * npm logout
 * @param {{npmHome:string, scope: string, registry:string}} args 
 */
const logout = (args) => {
    const cmd = `${args.npmHome}/npm logout --scope=${args.scope} --registry=${args.registry}`
    return exce(cmd);
}

/**
 * npm publish
 * @param {{ npmHome:string, registry:string, userconfig:string, filePath:string }} args 
 * @returns 
 */
const publish = (args) => {
    const {npmHome, registry, userconfig, filePath} = args;
    const cmd = `${npmHome}/npm publish --registry=${registry} --userconfig=${userconfig} ${filePath}`
    return exce(cmd)
}

export {
    login,
    logout,
    publish
}